let Compiler = require('./Compiler');

function webpack(options){
    //1.初始化参数：从配置文件和Shell语句中读取并合并参数,得出最终的配置对象
    let shellOptions = process.argv.slice(2).reduce((config,args)=>{
        let [key,value] = args.split('=');//--mode=production
        config[key.slice(2)]=value;
        return config;
    },{});//shellOptions={mode:'production'}
    let finalOptions = {...options,...shellOptions};
    //用上一步得到的参数初始化Compiler对象
    let compiler = new Compiler(finalOptions);
    //3.加载所有的配置的插件
    if(finalOptions.plugins && Array.isArray(finalOptions.plugins)){
        for(let plugin of options.plugins){
            plugin.apply(compiler);
        }
    }
    return compiler;
}

module.exports = webpack;